
import { put, call, takeEvery, all, fork } from 'redux-saga/effects';
import { ASDE_GET_TABLE_DATA, ASDEChangeTableLoading, ASDESetTableData, ASDE_GET_PERSON, ASDEChangePersonType, ASDESetPerson } from './actions';
import { tableList, getUrgePersonData } from '../api';

function* getTableData(action) {
    yield put(ASDEChangeTableLoading(true));
    try {
        const res = yield call(tableList, action.params);
        if(Number(res.code) === 200) {
            const { data } = res;
            yield put(ASDESetTableData(data))
        }
    } catch (e) {

    }
    yield put(ASDEChangeTableLoading(false));
}
function* watchGetTableData() {
    yield takeEvery(ASDE_GET_TABLE_DATA, getTableData)
}

//获取催收人
function* getPerson(action) {
    try{
        const res = yield call(getUrgePersonData, action.params);
        if(Number(res.code) === 200) {
            const { data: content } = res;
            const isGroup = content['type'] === 'group';
            const data = isGroup ? content['departmentList'] : content['mssAdminUserList'];
            const personData = data.map(item => ({ name: isGroup ? item['name'] : item.trueName, value: item.id }));
            yield put(ASDEChangePersonType(content['type']));
            yield put(ASDESetPerson(personData));
            action.callback && action.callback();
        }
    } catch (e) {
        console.log(e);
    }
}
function* watchGetPerson() {
    yield takeEvery(ASDE_GET_PERSON, getPerson);
}

export default function* root() {
    yield all([
        fork(watchGetTableData),
        fork(watchGetPerson)
    ]);
}
